package servlet;

import util.JdbcUtil;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class CustomerUpdateServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
         doPost(request,response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int cid= Integer.parseInt(request.getParameter("cid"));
        String name =request.getParameter("name");
        String person =request.getParameter("person");
        String tel =request.getParameter("name");
        String address =request.getParameter("address");
        String email =request.getParameter("email");
        String sql ="UPDATE customer set `name`=?,person=?,tel=?,address=?,email=? WHERE cid=?";
        try {
            PreparedStatement ps = JdbcUtil.getConnection().prepareStatement(sql);
            ps.setString(1,name);
            ps.setString(2,person);
            ps.setString(3,tel);
            ps.setString(4,address);
            ps.setString(5,email);
            ps.setInt(6,cid);
            int up = ps.executeUpdate();
            request.getRequestDispatcher("customer.jsp").forward(request, response);

        } catch (SQLException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

    }
}
